#!/bin/sh

## this script will re-encode audio files to
## ogg formatted files.
## %_loc: _home_/bin %

## $Id: rencode.sh 11 2007-10-06 03:09:03Z dhg $

display_usage () {

	VERSION_NUMBER=`grep "[$]mibsd" $0 | cut -d ' ' -f 4`
	echo ''
	echo "rencode version ${VERSION_NUMBER}"
	echo ''
	echo 'Usage:'
	echo ''
	echo '     rencode.sh in_format [out_format]'
	echo ''
	echo 'Note:'
	echo ''
	echo "    'out_format' can either be 'ogg' or 'mp3'."
	echo ''
	echo "    If you do not set 'out_format', all your"
	echo "    multimedia files will be encoded into 'ogg"
	echo "    vorbis' format!"
	echo ''
}

###########################

DUMPFILE=audiodump.wav
SOURCE=$1
TARGET=${2:-ogg}
DECODER=`which mplayer`
MONKEY_CMD=`which mac`
NORMALIZER=`which normalize`
NORM_OPT="-a -3dB"

if [ -n "${SOURCE}" ]; then

    for file in *.${SOURCE}; do

	if [ -r ${file} ];then
	    echo ''
	else
	    echo 'No input file?'
	    echo ''
	    exit 1
	fi
    done
else
    display_usage
    exit 1
fi

if [ -x "${NORMALIZER}" ]; then

    echo "Good! You have ${NORMALIZER} in your system..."
    echo 'Proceeding...'
    echo ''
else

    echo "You probably don't have 'normalize' ..."
    echo "sound normalizing will be disabled"
    echo ''
    NORMALIZER=`which false`
    NORM_OPT=
fi

if [ -x "${DECODER}" ]; then

    case "${TARGET}" in
	ogg)
	    ENCODER=`which oggenc`
	    if [ -x "${ENCODER}" ]; then
		echo ''
		echo "'oggenc' supports quality. What quality"
		echo "do you want your files to be?"
		echo -n "[-1 ... 10:]"
		read result
		OGG_QUAL=${result:-8}
		OGG_OPT="-q ${OGG_QUAL}"
	    else
		echo ''
		echo "You don\'t have 'oggenc' installed on"
		echo "your system. Qutting..."
		exit 1
	    fi
	    ;;
	mp3)
	    ENCODER=`which lame`
	    if [ -x ${ENCODER} ]; then
		echo ''
		echo "'lame' supports bitrate and modes."
		echo "What bitrate do you want your files to be?"
		echo -n "[32 ... 320]:"
		read BIT
		LAME_BITR="-b ${BIT:-128}"
		echo ''
		echo "What modes do you want your files to be?"
		echo "'s'tereo"
		echo "'j'oint stereo"
		echo "'f'orced joint stereo"
		echo "'d'ual channel"
		echo "'m'ono"
		echo -n "[s|j|f|d|m]:"
		read MODE
		echo ''
		echo 'Are your multimedia files encoded just for hardware players?'
		echo -n "[yes][no]"
		read FOR_HARDWARE_PLAYER
		case "${FOR_HARDWARE_PLAYER}" in
		    yes)
			HARD_OPT=-strictly-enforce-iso
			;;
		    *)
			HARD_OPT=
			;;
		esac
		LAME_MODE="-m ${MODE:-s}"
		LAME_OPT="${LAME_BITR} ${LAME_MODE} ${HARD_OPT}"
	    else
		echo ''
		echo "You don\'t have 'lame' installed on"
		echo 'your system. Quitting...'
		exit 1
	    fi
	    ;;
	*)
	    display_usage
	    exit 1
	    ;;
    esac

    case "${TARGET}" in
	ogg)
	    OPT=${OGG_OPT}
	    ;;
	mp3)
	    OPT=${LAME_OPT}
	    ;;
	*)
	    OPT=
	    ;;
    esac

    for file in *.${SOURCE}; do

	echo ''
	echo "Your files to be encoded from ${SOURCE} to ${TARGET}."
	echo "Your encoder is: `basename ${ENCODER}`."
	echo "The encoding options is: ${OPT}"
	echo ''
	echo -n "Your encoded file name will be: "
        echo  ${file%.${SOURCE}}.${TARGET}
	echo ''
	echo "Press Ctrl-c if I am wrong:)"
	echo "========================================================"
	echo ''
	sleep 3
	rm -f ${DUMPFILE}
	echo 'Now proceed to encode...'
	echo ''

	case "${SOURCE}" in
	    ape)
		if [ -x ${MONKEY_CMD} ]; then
		    ${MONKEY_CMD} ${SOURCE} ${DUMPFILE} -d
		else
		    echo "You need 'mac' to decode 'ape' files."
		    echo "Consider installing 'mac' before proceeding."
		    exit 1
		fi
		;;
	    wav)
		ln -s ${file} ${DUMPFILE}
		;;
	    *)
		${DECODER} -ao pcm:waveheader -vo null -vc null ${file}
		;;
	esac

        if [ ${NORMALIZER} ]; then
	    ${NORMALIZER} ${NORM_OPT} ${DUMPFILE}
        fi

	${ENCODER} ${OPT} ${DUMPFILE} -o ${file%.${SOURCE}}.${TARGET}
	rm -fr ${DUMPFILE}
    done
else
    echo "You need [mplayer] and [oggenc] or [lame] to proceed!"
    echo "Quitting ...!"
    exit 1
fi
